﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Infragistics.WebUI.UltraWebGrid;

public partial class page_DepotOut_Manager : PageBase
{
    private static DataTable dt = new DataTable();

    protected void Page_Load(object sender, EventArgs e)
    {
        // 记录SiteID,UltraWebGrid控件分页时_Site.ID的值会丢失。
        if (_Site != null)
        {
            txtSiteID.Text = _Site.ID.ToString();
        }

        this.RegisterJS();

        if (!IsPostBack)
        {
            InitComponents();
            ReadAction();
            BindData();
        }
    }

    #region Web 窗体设计器生成的代码

    protected override void OnLoad(EventArgs e)
    {
        isRequestLogin = true;                                  //是否需要登录
        RequestLoginPage = this.Request.Url.AbsoluteUri;        //获取决对Url
        isAtFramePageLogin = true;                              //是框架页

        //RequestCompetences = Competences.BuildCompetencesList(Competences.ZZCGD_48);//页面权限

        base.OnLoad(e);
    }

    #endregion

    protected void btnAdd_Click(object sender, EventArgs e)
    {        
        Response.Write("<script> parent.AddTab('DepotOut_Add',' 新增出库单','page/DepotOut_Edit.aspx?Action=Add');</script>");
    }

    protected void btnEdit_Click(object sender, EventArgs e)
    {
        string ID = hID.Value;

        Response.Write("<script> parent.AddTab('DepotOut_Edit','修改出库单','page/DepotOut_Edit.aspx?Action=Edit&ID=" + ID + "');</script>");
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        long ID = Shove._Convert.StrToLong(hID.Value,0);

        int i = DAL.Procedures.P_Shop_DelDepotOut_Master(ID);

        if (i >= 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, "删除订单成功！");
            this.BindData();
        }

        this.hID.Value = "";
    }

    protected void btnCheck_Click(object sender, EventArgs e)
    {
        long ID = Shove._Convert.StrToLong(hID.Value,0);                       //订单ID
        long VerfyOperID = _Customer.ID;                                       //操作会员ID
        string sRetrunDescripion = "";

        long i = DAL.Procedures.P_Shop_CheckDepotOut(ID, VerfyOperID, ref sRetrunDescripion);
        if (sRetrunDescripion != "")
        {
            Shove._Web.JavaScript.Alert(this.Page, sRetrunDescripion);

            return;
        }

        if (i >= 0)
        {
            Shove._Web.JavaScript.Alert(this.Page, "审核订单成功！");
        }

        this.BindData();
        this.hID.Value = "";
    }

    protected void btnExcel_Click(object sender, EventArgs e)
    {
        Utility.DataTableToExcel.DataTableToExcelAndDownload(dt, "Depot_Out_Manager.xls");
    }

    protected void btPrint_Click(object sender, EventArgs e)
    {
        Response.Write("<script language='javascript'>window.open('Depot_Out_Print.aspx?ID=" + hID.Value + "')</script>");

        DAL.Tables.tbl_Shop_PrintNum table = new DAL.Tables.tbl_Shop_PrintNum();

        long orderId = Shove._Convert.StrToLong((hID.Value), 0);                            //订单ID
        int num = Shove._Convert.StrToInt((hNum.Value), 0);                                 //最新次数
        string condition = "[Type]=1 and [OrderID]=" + orderId;


        if (num != 0)
        {
            table.Num.Value = num;
            table.Update(condition);     //更新打印次数
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string sCondition = "SourceShopID=" + _Site.ID.ToString();

        string sType = Shove._Web.Utility.GetRequest("Type");

        //单据号
        if (txtData.Text.Trim() != "")
        {
            sCondition += " and OutNo='" + txtData.Text.Trim() + "'";
        }

        //时间
        if (txtBeginDate.Text != "" && txtEndDate.Text != "")
        {
            DateTime dtStart = Shove._Convert.StrToDateTime(txtBeginDate.Text, DateTime.Now.ToString("yyyy-MM-dd"));
            DateTime dtEnd = Shove._Convert.StrToDateTime(txtEndDate.Text, DateTime.Now.ToString("yyyy-MM-dd"));
            TimeSpan ts = dtStart - dtEnd;

            if (ts.Days > 0 || ts.Hours > 0 || ts.Minutes > 0)
            {
                Shove._Web.JavaScript.Alert(this.Page, "开始时间晚于结束时间！");

                return;
            }
            else
            {
                sCondition += " and " + ddlDateType.SelectedValue + " between '" + dtStart.ToString("yyyy-MM-dd") + "' and '" + dtEnd.AddDays(1).ToString("yyyy-MM-dd") + "'";
            }
        }

        dt = new DAL.Views.V_Shop_DepotOutMaster().Open("", Shove._Web.Utility.FilteSqlInfusion(sCondition), "");

        uwgMain.DataSource = dt;
        uwgMain.DataBind();
    }

    protected void uwgMain_PageIndexChanged(object sender, Infragistics.WebUI.UltraWebGrid.PageEventArgs e)
    {
        this.BindData();
    }

    protected void uwgMain_ActiveRowChange(object sender, RowEventArgs e)
    {
        string sType = Shove._Web.Utility.GetRequest("Type");
        if (sType == "Query")
        {
            string sID = this.uwgMain.Rows[e.Row.Index].Cells[0].Value.ToString();
            this.BindDataDetail(sID);
            trDetail.Visible = true;
        }
    }

    #region

    private void RegisterJS()
    {
        //AjaxPro.Utility.RegisterTypeForAjax(typeof(Ajax.PrintNumCheck));

        Infragistics.WebUI.Shared.Util.ClientScript.ResolveAndRegisterClientScriptInclude(
             Shove._Web.Utility.GetUrl() + "/js/Scripts/ig_webgrid_an.js",
             "",
             Shove._Web.Utility.GetUrl() + "/js/Scripts/ig_webgrid_an.js",
             "ig_webgrid_an.js", this.uwgMain, "ig_webgrid_an");          //注册 Infragistics js和控件        
    }

    private void ReadAction()
    {
        string sType = Request.QueryString["Type"];

        if (sType == "New")
        {
            btnAdd.Visible = true;
            btnEdit.Visible = true;
            btnDel.Visible = true;
            btnCheck.Visible = false;
            btnExcel.Visible = false;
            btnPrint.Visible = false;
        }
        else if (sType == "Check")
        {
            btnAdd.Visible = false;
            btnEdit.Visible = false;
            btnDel.Visible = false;
            btnCheck.Visible = true;
            btnExcel.Visible = false;
            btnPrint.Visible = false;
        }
        else if (sType == "Query")
        {
            btnAdd.Visible = false;
            btnEdit.Visible = false;
            btnDel.Visible = false;
            btnCheck.Visible = false;
            btnExcel.Visible = true;
            btnPrint.Visible = true;
        }
    }

    private void BindData()
    {
        if (dt != null && dt.Rows.Count > 0)
        {
            dt.Clear();
        }

        string sType = Request.QueryString["Type"];
        long SiteID = Shove._Convert.StrToLong(txtSiteID.Text, -1);

        if (sType == "New")
        {
            dt = new DAL.Views.V_Shop_DepotOutMaster().Open("", "CreateShopID=" + SiteID.ToString(), "");
        }
        else if (sType == "Check")
        {
            dt = new DAL.Views.V_Shop_DepotOutMaster().Open("", "CreateShopID=" + SiteID.ToString(), "");
        }
        else if (sType == "Query")
        {
            dt = new DAL.Views.V_Shop_DepotOutMaster().Open("", "CreateShopID=" + SiteID.ToString(), "");
        }

        uwgMain.DataSource = dt;
        uwgMain.DataBind();
    }

    private void InitComponents()
    {
        txtBeginDate.Attributes.Add("onclick", "WdatePicker()");
        txtEndDate.Attributes.Add("onclick", "WdatePicker()");

        Utility.InitUltraWebGrid.InitDisplayLayOut(this.uwgMain);
        Utility.InitUltraWebGrid.InitDisplayLayOut(this.uwgDetail);
    }

    /// <summary>
    /// 加载该订单下的所有商品信息
    /// </summary>
    private void BindDataDetail(string sID)
    {
        if (dt != null && dt.Rows.Count > 0)
        {
            dt.Clear();
        }

        dt = new DAL.Tables.tbl_Shop_DepotOutDetail().Open("", "OutID=" + sID, "");

        uwgDetail.DataSource = dt;
        uwgDetail.DataBind();
    }

    /// </summary>
    /// 设置页面不被缓存
    /// </summary>
    public void SetPageNoCache()
    {
        HttpContext.Current.Response.Buffer = true;
        HttpContext.Current.Response.Expires = 0;
        HttpContext.Current.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
        HttpContext.Current.Response.AddHeader("pragma", "no-cache");
        HttpContext.Current.Response.AddHeader("cache-control", "private");
        HttpContext.Current.Response.CacheControl = "no-cache";
    }

    #endregion
}
